package com.jelly.web.modules.basics.dictionary.service.impl;

import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.basics.dictionary.dao.DiyTypeMapper;
import com.jelly.web.modules.basics.dictionary.domain.DiyType;
import com.jelly.web.modules.basics.dictionary.domain.DiyTypeExample;
import com.jelly.web.modules.basics.dictionary.service.IDiyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-08-17 10:40
 * @功能描述: 字典类型接口实现类
 **/
@Service
public class DiyTypeServiceImpl implements IDiyTypeService {

    @Autowired
    private DiyTypeMapper diyTypeMapper;

    @Override
    public DiyType getDiyTypeRoute(String diyTypeRoute) {

        DiyTypeExample example = new DiyTypeExample();

        example.createCriteria().andDiyTypeRouteEqualTo(diyTypeRoute);

        List<DiyType> list = diyTypeMapper.selectByExample(example);

        if(ListUtil.isNotList(list)){
            return list.get(0);
        }

        return null;
    }

    /**
     * 主键查询字典类型
     * @param id
     * @return
     */
    @Override
    public DiyType getDiyTypeId(String id) {
        return diyTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过状态获取类型
     * @param isState
     * @return
     */
    @Override
    public List<DiyType> getDiyType(String isState) {
        DiyTypeExample example = new DiyTypeExample();

        example.createCriteria().andIsStateEqualTo(isState);

        return diyTypeMapper.selectByExample(example);
    }
    /**
     * 新增字典类型
     * @param diyType
     * @return
     */
    @Override
    public boolean addDiyType(DiyType diyType) {
        diyType.setDiyTypeId(UUID.randomUUID().toString().replaceAll("-", ""));
        diyType.setAddDate(new Date());
        diyType.setAddId(TokenUtil.getUserId());
        return diyTypeMapper.insertSelective(diyType) > 0;
    }

    @Override
    public boolean updateDiyType(DiyType diyType) {
        if(StringUtils.isBlank(diyType.getDiyTypeId())){
            diyType.setDiyTypeId(diyType.getBaseId());
        }
        diyType.setUpdateDate(new Date());
        diyType.setUpdateId(TokenUtil.getUserId());
        return diyTypeMapper.updateByPrimaryKeySelective(diyType) > 0;
    }

    @Override
    public boolean delectDiyType(String id) {
        return diyTypeMapper.deleteByPrimaryKey(id) > 0;
    }
}
